//
//  LXTabBarButton.m
//  微博
//
//  Created by imera on 15-9-11.
//  Copyright (c) 2015年 imera. All rights reserved.
//

/***
 * 自动调整控件的位置
 * badgeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
 *
 ***/

//设置图片比例
#define bili 0.7
// 按钮的默认文字颜色
#define  LXTabBarButtonTitleColor (IOS7 ? [UIColor blackColor] : [UIColor whiteColor])
// 按钮的选中文字颜色
#define  LXTabBarButtonTitleSelectedColor (IOS7 ? LXColor(234, 103, 7) : LXColor(248, 139, 0))

#import "LXTabBarButton.h"
#import "LXBadgeBtn.h"

@interface LXTabBarButton()

@property (nonatomic, weak) LXBadgeBtn *badgeBtn;

@end

@implementation LXTabBarButton
//设置item
- (void)setItem:(UITabBarItem *)item
{
    _item = item;
    
    [self setTitle:item.title forState:UIControlStateNormal];
    [self setImage:item.image forState:UIControlStateNormal];
    [self setImage:item.selectedImage forState:UIControlStateSelected];
    if(!IOS7){
        [self setBackgroundImage:[UIImage imageWithName:@"tabbar_slider"] forState:UIControlStateSelected];
    }
    //设置提醒数字
    self.badgeBtn.badge = self.item.badgeValue;
    
    [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];
    
    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
}
///
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //图片居中
        self.imageView.contentMode = UIViewContentModeCenter;
        //文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        //文字大小
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        //文字颜色
        [self setTitleColor:LXTabBarButtonTitleColor forState:UIControlStateNormal];
        //文字选中颜色
        [self setTitleColor:LXTabBarButtonTitleSelectedColor forState:UIControlStateSelected];
        
        //添加提示数字
        LXBadgeBtn *badgeBtn = [[LXBadgeBtn alloc]init];
        //设置拉伸模式
        self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
        
        self.badgeBtn = badgeBtn;
        [self addSubview:badgeBtn];

    }
    return self;
}

//重写高亮状态
- (void)setHighlighted:(BOOL)highlighted{}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageW = contentRect.size.width;
    CGFloat imageH = contentRect.size.height * bili;
    
    return CGRectMake(imageX, imageY, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = contentRect.size.height * bili;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height * (1-bili);
    
    return CGRectMake(titleX, titleY, titleW, titleH);
}

//某个对象的属性发生变化了就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    self.badgeBtn.badge = self.item.badgeValue;

    //设置badgeButton的位置
    CGRect frame = self.badgeBtn.frame;
    
    frame.origin.x = 40;
    frame.origin.y = 0;
    
    self.badgeBtn.frame = frame;

}

@end
